﻿using Newtonsoft.Json;
using ThresholdAssistant.DataContract.Extensions;
using ThresholdAssistant.Enums;
using System;
using System.Text.Json.Serialization;

namespace ThresholdAssistant.DataContract.Result
{
    /// <summary>
    /// 风控系统返回值
    /// </summary>
    [Serializable]
    public class RiskContractResutl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public RiskContractResutl()
        {
            Success = false;
            ErrorCode = $"{(int)ErrorTypeEnum.BusinessFailed}";
            ErrorMessage = ErrorTypeEnum.BusinessFailed.GetDescription();
        }

        /// <summary>
        /// 操作结果
        /// </summary>
        [JsonProperty("success"), JsonPropertyName("success")]
        public bool Success { get; set; }

        /// <summary>
        /// 错误状态码
        /// </summary>
        [JsonProperty("errorCode"), JsonPropertyName("errorCode")]
        public string ErrorCode { get; set; }

        /// <summary>
        /// 错误信息，成功将返回空
        /// </summary>
        [JsonProperty("errorMessage"), JsonPropertyName("errorMessage")]
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 业务流水号
        /// </summary>
        [JsonProperty("serial_no"), JsonPropertyName("serial_no")]
        public string SerialNo { get; set; } = Guid.NewGuid().ToString("N").ToUpper();

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="enumType">错误信息</param>
        public RiskContractResutl SetError<T>(T enumType) where T : Enum
        {
            Success = false;
            ErrorCode = enumType.GetHashCode().ToString();
            ErrorMessage = enumType.GetDescription();
            return this;
        }

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="enumType">错误信息</param>
        /// <param name="serialNo">业务流水号</param>
        public RiskContractResutl SetError<T>(T enumType, string serialNo = "") where T : Enum
        {
            Success = false;
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            ErrorMessage = enumType.GetDescription();
            ErrorCode = enumType.GetHashCode().ToString();
            return this;
        }

        /// <summary>
        /// 设置错误信息
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="message">错误消息</param>
        /// <param name="serialNo"></param>
        public RiskContractResutl SetError(string code, string message, string serialNo = "")
        {
            Success = false;
            ErrorCode = code;
            ErrorMessage = message;
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            return this;
        }

        /// <summary>
        /// 检查是否成功
        /// </summary>
        public bool IsSuceccful()
        {
            return Success == true;
        }

        /// <summary>
        /// 设置成功返回信息
        /// </summary>
        public RiskContractResutl SetSuceccful(string serialNo = "")
        {
            Success = true;
            if (!string.IsNullOrWhiteSpace(serialNo))
            {
                SerialNo = serialNo;
            }
            ErrorMessage = ErrorTypeEnum.SUCCESS.GetDescription();
            ErrorCode = ErrorTypeEnum.SUCCESS.GetHashCode().ToString();
            return this;
        }

        /// <summary>
        /// 返回值
        /// </summary>
        [JsonProperty("data"), JsonPropertyName("data")]
        public RiskContractData Data { get; set; }
    }

    /// <summary>
    /// 风控系统返回值
    /// </summary>
    [Serializable]
    public class RiskContractData
    {
        /// <summary>
        /// 操作结果
        /// </summary>
        [JsonProperty("auditResult"), JsonPropertyName("auditResult")]
        public bool AuditResult { get; set; }

        /// <summary>
        /// 返回值
        /// </summary>
        [JsonProperty("data"), JsonPropertyName("data")]
        public object Data { get; set; }
    }
}